package com.ncu.musicbox2020;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.provider.MediaStore;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class MusicMannager {
    private List<MusicInfo> musiclist = new ArrayList<MusicInfo>();
    private Random random;
    private MediaPlayer mPlayer;
    private Context context;
    private int seekLength = 0;
    private int currentIndex = -1;
    private int total_music;

    public MusicMannager(Context context){
        this.context = context;
        random = new Random(System.currentTimeMillis());
        ResolveMusicToList();
        initPlayer();
    }
    public void initPlayer(){
        mPlayer = new MediaPlayer();
        mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    }
    public void Release(){//释放
        mPlayer.reset();
        mPlayer.stop();
        mPlayer.release();
    }
    public void Pause(){//暂停
        if(mPlayer.isPlaying()){
            mPlayer.pause();
            seekLength = mPlayer.getCurrentPosition();//获得当前的位置并且为这个实例储存下来
        }
    }
    public void Resume(){//恢复
        //修改定位位置
        mPlayer.seekTo(seekLength);
        mPlayer.start();
    }
    public void SetSeekPos(int seekPos){//修改播放位置
        mPlayer.seekTo(seekPos);
    }
    public void Reset(){//播放复位
        seekLength = 0;
        mPlayer.seekTo(seekLength);
    }
    public void Play(){//播放
        mPlayer.reset();
        Uri path = Uri.parse(musiclist.get(currentIndex).getMusic_path());
        try{
            mPlayer.setDataSource(String.valueOf(path));
            mPlayer.prepare();
        } catch (IOException e) {
            e.printStackTrace();
        }
        mPlayer.seekTo(seekLength);
        mPlayer.start();
    }
    //顺序
    public void PlayNext(){
        currentIndex = currentIndex + 1;
        if(currentIndex>musiclist.size()){
            currentIndex = 0;
        }
        seekLength = 0;
        if(mPlayer.isPlaying()){
            Play();
        }
    }
    public void PlayPrev(){
        currentIndex = currentIndex - 1;
        if(currentIndex<0){
            currentIndex = musiclist.size()-1;
        }
        seekLength = 0;
        if(mPlayer.isPlaying()){
            Play();
        }
    }
    //单曲
    public void PlayThis(){
        //播放的还是这个音乐
        currentIndex = currentIndex;
        seekLength = 0;
        if(mPlayer.isPlaying()){
            Play();
        }
    }
    //随机
    public void PlayRandom(){
        Random random = new Random();
        int rand = random.nextInt(total_music);
        currentIndex = rand;
        if(currentIndex>musiclist.size()){
            currentIndex = 0;
        }
        seekLength = 0;
        if(mPlayer.isPlaying()){
            Play();
        }
    }
    public boolean isPlaying(){
        return mPlayer.isPlaying();
    }
    public int getDuration(){
        return mPlayer.getDuration();
    }
    public int getCurrentPosition(){
        return mPlayer.getCurrentPosition();
    }
    public void seekTo(int length){
        seekLength = length;
        mPlayer.seekTo(length);
    }
    public void setCurrentIndex(int index){
        currentIndex = index;
    }
    public int getCurrentIndex(){
        return currentIndex;
    }
    MusicInfo getMusicInfo(int index){
        return musiclist.get(index);
    }
    public List<MusicInfo> getMusiclist(){
        return musiclist;
    }
    public int getTotal_music(){
        return total_music;
    }
    public void ResolveMusicToList(){
        String selection = MediaStore.Audio.Media.IS_MUSIC + "!=0";
        String sortOrder = MediaStore.MediaColumns.DISPLAY_NAME+"";
        //查询的列名字
        String[] projection = {MediaStore.Audio.Media.TITLE,MediaStore.Audio.Media.ARTIST,MediaStore.Audio.Media.DISPLAY_NAME,MediaStore.Audio.Media.DATA,MediaStore.Audio.Media.DURATION};
        //利用ContentResolver得到ContextProvider接口封装的开放数据
        ContentResolver contentResolver = context.getContentResolver();
        Cursor cursor = contentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,projection,selection,null,sortOrder);
        if(cursor!=null){
            total_music = cursor.getCount();
            for(cursor.moveToFirst();cursor.isAfterLast()!=true;cursor.moveToNext()){
                MusicInfo mInfo = new MusicInfo();
                mInfo.setMusic_title(cursor.getString(0));
                mInfo.setMusic_artist(cursor.getString(1));
                mInfo.setMusic_name(cursor.getString(2));
                mInfo.setMusic_path(cursor.getString(3));
                mInfo.setMusic_duration(Integer.parseInt(cursor.getString(4)));
                musiclist.add(mInfo);
            }
        }
    }

}
